<!DOCTYPE html>
<html lang="en" style="transition: background-color 0.15s ease 0s;">
<head>
    {% load static %}
    <meta charset="UTF-8">
    <title>接口列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{% static '/css/layui.css' %}">
    <link rel="stylesheet" href="{% static '/css/modules/layer/default/layer.css' %}">
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div class="layui-header header header-demo" style="background-color: #393D49">
        <div class="layui-main">
            <ul class="layui-nav">
                <button style="background-color: #393D49;margin-left: -20px;" type="button"
                        class="layui-btn layui-btn-normal"><i
                        class="layui-icon"></i></button>
                <li class="layui-nav-item"><a href="{%url 'TestPlatform_User:Home'%}"><h1>TestPlatform</h1></a></li>
                <li class="layui-nav-item layui-this">
                    <a href="{%url 'TestPlatform_User:Project_List'%}">项目列表</a>
                <li class="layui-nav-item">
                    <a href="{%url 'TestPlatform_User:Interface_List'%}">接口列表</a>
                </li>
                <li class="layui-nav-item">
                    <a href="{%url 'TestPlatform_User:Interface_Perform'%}">快速执行</a>
                </li>
                <li class="layui-nav-item"><a href="">测试报告</a></li>
                <li class="layui-nav-item"><a href="">邮件配置</a></li>
                <li class="layui-nav-item" style="float: right">
                    <a href="javascript:;">{{ user.get_username }}</a>
                    <dl class="layui-nav-child">
                        <dd><a href="{%url 'TestPlatform_User:Logout'%}"><i
                                class="layui-icon">&#xe64d; </i>登出</a></dd>
                    </dl>
                </li>
                <li style="margin:12px;float: right">
                    <div style="margin-left: 30px;">
                        <input type="hidden" name="color" value="" id="test-all-input">
                        <div id="test-all"></div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
    <div style="padding: 30px; background-color: #F2F2F2;">
        <div class="layui-upload demoTable ">
            <button type="button" class="layui-btn" id="upload" name="file" style="margin-bottom: 7px"><i
                    class="layui-icon"></i>上传用例
            </button>
            <button type="button" class="layui-btn layui-btn-danger" id="batchDelete" data-type="getCheckData"
                    style="float: right; margin-right: 20px"><i class="layui-icon">&#xe640;</i>批量删除
            </button>
            <!--<button type="button" class="layui-btn layui-btn-warm" id="addInterface"-->
            <!--style="float: right;margin-right: 20px">新增用例-->
            <!--</button>-->
            <button type="button" class="layui-btn layui-btn-primary layui-border-green" id="batchPerform"
                    data-type="batchPerform"
                    style="float: right; margin-right: 20px"><i class="layui-icon">&#xe605;</i>批量执行
            </button>
            <div class="layui-inline ">
                <input type="text" class="layui-input" name="apiurl" placeholder="输入Yapi接口地址" title="输入Yapi接口地址"
                       id="yapiurl" autocomplete="off"
                       style="margin-left: 797px;margin-bottom: 6px;width: 450px">
            </div>
            <button type="button" class="layui-btn layui-btn-primary layui-border-green" id="getApiData"
                    data-type="apiData"
                    style="float: right; margin-right: 20px"><i class="layui-icon">&#xe605;</i>Yapi接口导入
            </button>
            <div style="text-align: center;margin-top: -40px;margin-left: 237px;"><h1>{{ pj_name }}</h1></div>

        </div>
        <div class="demoTable" style="margin-top: -45px; margin-left: 100px">
            <div class="layui-inline ">
                <input type="text" class="layui-input" name="id" placeholder="可根据id进行搜索"
                       title="可根据id进行搜索" id="demoReload" autocomplete="off"
                       style="margin-left: 50px;margin-bottom: 2px">
            </div>
            <button class="layui-btn" data-type="reload" style="margin-left: 50px;">搜索</button>

        </div>
        <!--        <div class="demoTable" style="margin-top: -41px; margin-left: 500px">-->
        <!--            &lt;!&ndash;            <div class="layui-inline ">&ndash;&gt;-->
        <!--            &lt;!&ndash;                <input type="text" class="layui-input" name="yapiurl" placeholder="请输入Yapi链接地址">&ndash;&gt;-->
        <!--            &lt;!&ndash;            </div>&ndash;&gt;-->
        <!--            <button type="button" class="layui-btn layui-btn-primary layui-border-green">-->
        <!--                Yapi接口导入-->
        <!--            </button>-->
        <!--        </div>-->
        <table class="layui-hide" id="table_data" lay-filter="table_data"></table>
        <script type="text/html" id="toolbar">
            <a class="layui-btn layui-btn-xs" lay-event="perform"
               href="{%url 'TestPlatform_User:Interface_Perform'%}">执行</a>
            <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="edit">修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
</div>


{% load static %}
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script src="{% static '/js/layui.js' %}" charset="utf-8"></script>
<script src="{% static '/js/lay/modules/upload.js' %}" charset="utf-8"></script>
<!--<script src="{% static '/js/lay/modules/table.js' %}" charset="utf-8"></script>-->
<!--table.js中搜索i.data("edit")改为dblclick-->
<script>
    layui.use('element', function () {
        var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
        //监听导航点击
        element.on('nav(table_data)', function (elem) {
            //console.log(elem)
            layer.msg(elem.text());
        });
    });

    layui.use('table', function () {
        var table = layui.table;
        table.on('edit(table_data)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            layer.confirm('确定将信息修改为：' + value, {
                btn: ['确定', '取消']
            }, function (index) {
                $.ajax({
                    type: 'post',
                    url: "{%url 'TestPlatform_User:Update_Data_If'%}",
                    data: {'data_id': data.id, 'field': field, 'value': value},
                    traditional: true,
                    async: false,
                    success: function (result) {
                    },
                    error: function (result) {
                    }
                });
                // window.location.reload();
                layer.close(index);
            }, function () {
                window.location.reload();
                layer.close(index);
            })
        });

        table.render({
            elem: '#table_data'	//表单ID
            , url: "{%url 'Api:project_data'%}"	//数据接口
            , page: true			//分页设置
            , limit: 20		//默认每一页显示数量
            , limits: [10, 20, 30, 40, 50]		//可选择每页显示的数量
            , toolbar: true
            , cols: [[			//表格列
                {type: 'checkbox', sort: false}
                , {field: 'in_id', title: 'ID', align: 'center', sort: true, edit: 'int'}
                , {field: 'in_mname', title: '接口名称', sort: false, edit: 'text'}
                , {field: 'in_type', title: '接口类型', align: 'center', sort: false, edit: 'text'}
                , {field: 'in_url', title: '接口地址', sort: false, edit: 'text'}
                , {field: 'in_data', title: '接口参数', sort: false, edit: 'text'}
                , {field: 'in_data_type', title: '数据类型', align: 'center', sort: false, edit: 'text'}
                , {field: 'in_tname', title: '测试人员', align: 'center', sort: false, edit: 'text'}
                , {field: 'in_expected_result', title: '预期结果', sort: false, edit: 'text'}
                , {field: 'dobar', title: '操作', align: 'center', width: 200, sort: false, toolbar: '#toolbar'}
            ]]
            , response: {
                statusCode: 200
            }
            , id: 'testReload'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                var result;
                // console.log(this);
                // console.log(JSON.stringify(res));
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        });
        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload');
                if (demoReload.val() === "") {
                    location.reload();
                } else {
                    //执行重载
                    table.reload('testReload', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            id: demoReload.val(),
                        }
                    });
                }
            },
            apiData: function () {
                var yapiurl = $('#yapiurl').val();
                $.ajax({
                    type: 'post',
                    url: "{%url 'TestPlatform_User:GetApi'%}",
                    data: {"yapiurl": yapiurl},
                    traditional: true,
                    async: false,
                    success: function () {
                        layer.msg(' 用例导入成功! ');
                    },
                    error: function () {
                        layer.msg(' 用例导入失败!请重新检查地址! ');
                    },
                });
            },
            getCheckData: function () { //获取选中数据
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                var arr = JSON.stringify(data);
                if (data.length === 0) {
                    layer.msg('请选择需要删除的数据！')
                } else {
                    layer.confirm('是否删除选中的' + data.length + '条数据？', function (index) {
                        $.ajax({
                            type: 'post',
                            url: "{%url 'TestPlatform_User:Batch_Delete_If'%}",
                            data: {'arr': arr},
                            traditional: true,
                            async: false,
                        });
                        window.location.reload();
                    });
                }
            }
            , batchPerform: function () {
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                var arr = JSON.stringify(data);
                if (data.length === 0) {
                    layer.msg('请选择需要执行的数据！')
                } else {
                    $.ajax({
                        type: 'POST',
                        url: "{%url 'TestPlatform_User:Batch_Perform_If'%}",
                        data: {'arr': arr},
                        traditional: true,
                        async: false,
                    });
                    $.ajax({
                        type: 'POST',
                        url: "{%url 'Api:perform_data'%}",
                        data: {'arr': arr},
                        traditional: true,
                        async: false,
                    });
                    window.location = "http://localhost:8000/perform_result/";
                    // window.location.reload();
                }
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });


    //上传功能
    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;
        var storage = window.sessionStorage;
        id = storage.id;
        upload.render({
            elem: '#upload'
            , url: "{% url 'TestPlatform_User:Pj_UploadExcel' %}" //改成自己的上传路径
            , accept: 'file'
            , auto: true
            , data: {     //layui upload传参方式
                id: id
            }
            , done: function (res) {
                console.log(res);
                if ('{{message}}' === '导入失败') {
                    layer.msg('上传失败');
                } else {
                    // layer.load(1, {time: 10 * 100});
                    layer.msg('上传成功');
                    setTimeout("window.location.reload()", 700);
                }
            }
        });
    });
    layui.use('table', function () {
        var table = layui.table;
        //监听表格复选框选择
        table.on('checkbox(table_data)', function (obj) {
            console.log(obj)
        });
        //监听工具条 删除一条数据
        table.on('tool(table_data)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: 'post',
                        url: "{%url 'TestPlatform_User:Delete_Data'%}",
                        data: {'data_id': data.id},
                        traditional: true,
                        async: false,
                        success: function (result) {
                        },
                        error: function (result) {
                        }
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.msg('双击单元格试试!');
            } else if (obj.event === 'perform') {  //选中一条数据点击执行，使用sessionStorage存储数据
                var storage = window.sessionStorage;
                storage['in_url'] = data.in_url;
                storage['in_data'] = data.in_data;
                storage['in_type'] = data.in_type;
                storage['in_data_type'] = data.in_data_type;
                storage['in_expected_result'] = data.in_expected_result;
            }
        });
    });

</script>
</body>
</html>
